<template>
	<view class="goods_item">
		<view class="good_img_cont">
			<image class="good_img" :src="img" mode=""></image>
		</view>
		<view class="good_info">
			<view class="good_title">
				{{title}}
			</view>
			<view class="good_sub">
				{{dec}}
			</view>
			<view class="add_price_cont">
				<view class="prcie_cont">
					￥{{parseFloat(price).toFixed(2)}}

				</view>
				<view class="numcont">
					<view style="font-size:28rpx; color: #555555; display: flex; align-items: center;" v-if="num" @tap="minusnum">一</view>
					<text v-if="num" class="num">{{num}}</text>
					<u-icon name="plus-circle-fill" color="#E02E24" size="45" @tap="addnum"></u-icon>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				selected: [],
				num: 0,
				totalNum: 0,
				totalPrice: 0,
			}
		},
		onReady() {
			var _this = this
			setTimeout(() => {
				uni.getStorage({
					key: "columngood",
					success(res) {
						var arr = JSON.parse(res.data)
						arr.map((item, index) => {
							if (item.id == _this.$props.goodId) {
								_this.num = item.num
							}
						})
					}
				})
			}, 80)
		},

		// filters:{
		// 	Int:function(value){
		// 		return parseInt(value)
		// 	},
		// 	duts:function(value){
		// 		return value * 100%100
		// 	}
		// },
		props: {
			title: {
				type: String,
				default: "产品主标题"
			},
			dec: {
				type: String,
				default: "在这里阐述产品的特点"
			},
			price: {

				default: 12222.5
			},
			img: {},
			goodId: {},

		},
		methods: {
			addnum() {
				this.num++
				this.numCount("+")
				this.totalComputed()
				this.$emit("count", this.num)
			},
			minusnum() {
				this.num > 0 ? this.num-- : 0
				this.numCount("-")
				this.totalComputed()
				this.$emit("count", this.num)
			},

			numCount(character) {
				var countnum = this.num
				var {
					goodId,
					price
				} = this.$props
				//获取本地存储内的商品
				uni.getStorage({
					key: "columngood",
					success(res) {
						//成功获取，非首次添加商品
						var arr = JSON.parse(res.data)
						var isNo = false //列表内是否含有待选择物品
						arr.map((item, index) => {
							//判断是加还是减少，取决于调用时传参
							if (item.id == goodId) {
								if (character == "+") {
									item.num++
								} else {
									item.num--
									if (item.num <= 0) {
										arr.splice(index, 1)
									}
								}
								isNo = true
							}
						})
						if (isNo) {
							console.log(arr.length)
							if (!arr.length) {
								uni.removeStorageSync('columngood')
								return
							}
							uni.setStorage({
								key: "columngood",
								data: JSON.stringify(arr)
							})
						} else {
							var obj = {
								id: goodId,
								price,
								num: countnum
							}
							arr.push(obj)
							uni.setStorage({
								key: "columngood",
								data: JSON.stringify(arr)
							})
						}

					},
					fail() {

						var selected = []

						var obj = {
							id: goodId,
							price,
							num: countnum
						}
						selected.push(obj)
						uni.setStorage({
							key: "columngood",
							data: JSON.stringify(selected)
						})

					}
				})
			},
			//计算总价及数量
			totalComputed() {
				uni.getStorage({
					key: "columngood",
					success(data) {
						console.log(data.data)
					}
				})
			}

		}
	}
</script>

<style scoped lang="scss">
	.goods_item {
		margin: 10rpx 5rpx;
		background: #fff;
		display: flex;
		flex-direction: column;
		border: 1px solid #F8F8F8;
		padding-bottom: 32rpx;
		border-radius: 10rpx;

		.good_img_cont {
			height: 325rpx;
			width: 100%;

			.good_img {
				height: 100%;
				width: 100%;
			}
		}

		.good_info {
			width: 100%;

			box-sizing: border-box;
			padding: 10rpx 20rpx 0 15rpx;
		}

		.good_title {
			font-size: 29rpx;
			font-weight: 700;
			word-break: break-word; //换行模式
			overflow: hidden;
			text-overflow: ellipsis; //修剪文字
			display: -webkit-box;
			-webkit-line-clamp: 1; //此处为上限行数
			-webkit-box-orient: vertical;

		}

		.good_sub {
			font-size: 24rpx;
			color: #8E8E8E;
			word-break: break-word; //换行模式
			overflow: hidden;
			text-overflow: ellipsis; //修剪文字
			display: -webkit-box;
			-webkit-line-clamp: 1; //此处为上限行数
			-webkit-box-orient: vertical;
		}

		.add_price_cont {
			margin-top: 16rpx;
			width: 100%;
			display: flex;
			justify-content: space-between;

			.numcont {
				display: flex;
				position: relative;
				bottom: 10rpx;

				text {
					display: inline-flex;
					font-size: 28rpx;
					margin: 0 15rpx;
					align-items: center;
				}
			}

			.prcie_cont {
				color: #F84E4E;
				font-size: 26rpx;
				font-weight: 700;

				.dut {
					font-size: 22rpx;
					font-weight: normal;
				}
			}
		}
	}
</style>
